package com.czn.service.impl;

import com.czn.dao.DrugsDao;
import com.czn.entity.Drugs;
import com.czn.service.IDrugsService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ChenZeNan
 * @create 2021-04-18 0:15
 */
@Service
public class DrugsServiceImpl implements IDrugsService {
    @Autowired
    private DrugsDao drugsDao;

    /**
     * 分页/模糊查询药品
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @Override
    public PageQuery<Drugs> findDrugsList(Integer pageNo, Integer pageSize, String name, Integer type) {
        LambdaQuery<Drugs> query = drugsDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            query.andLike(Drugs::getName, "%" + name + "%");
        }
        if (type != null) {
            query.andEq(Drugs::getType, type);
        }
        if (pageNo > 0 && pageSize > 0) {
            PageQuery<Drugs> page = query.desc(Drugs::getId).page(pageNo, pageSize);
            return page;
        }
        return null;
    }

    /**
     * 根据药品id查询药品
     *
     * @param id
     * @return
     */
    @Override
    public Drugs findDrugById(Integer id) {
        Drugs drug = drugsDao.single(id);
        return drug;
    }

    /**
     * 新增药品
     *
     * @param drug
     * @return
     */
    @Override
    public ResponseResult addDrug(Drugs drug) {
        ResponseResult result = new ResponseResult();
        // 根据药品名称查询数据库中是否已有该对象了
        LambdaQuery<Drugs> query = drugsDao.createLambdaQuery();
        query.andEq(Drugs::getName, drug.getName());
        Drugs sysDrug = query.single();
        if (sysDrug == null) { // 数据库中没有该对象，直接插入
            drugsDao.insert(drug);
            result.setCode("300");
            result.setMessage("信息添加成功！");
            return result;
        }
        // 数据库中已有该对象了
        result.setCode("301");
        result.setMessage("该药品信息已存在数据库中~");
        return result;
    }

    /**
     * 编辑药品
     *
     * @param drug
     * @return
     */
    @Override
    public ResponseResult editDrug(Drugs drug) {
        ResponseResult result = new ResponseResult();
        int rows = drugsDao.updateById(drug);
        if (rows > 0) {
            result.setCode("500");
            result.setMessage("信息更新成功！");
            return result;
        }
        result.setCode("501");
        result.setMessage("信息更新失败！");
        return result;
    }

    /**
     * 删除药品
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDrugById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = drugsDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }

    /**
     * 查找所有药品记录
     *
     * @return
     */
    @Override
    public List<Drugs> findAll() {
        return drugsDao.all();
    }
}
